package com.apes.fn.fico.reimbursement.model;

import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;

/**
 * @BelongsProject: apes-server
 * @BelongsPackage: com.apes.fn.fico.reimbursement.model
 * @ClassName: ReimbursementDebit
 * @Author: pyb
 * @Description:
 * @CreateTime: 2021-08-27 11:44
 * @Version: 1.0
 */
@Getter
@Setter
@SelectBeforeUpdate
@DynamicUpdate
@DynamicInsert
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = ReimbursementDebit.CONST_TABLE_NAME)
public class ReimbursementDebit extends BaseModel {
    public static final String CONST_TABLE_NAME = "fn_reimbursement_debit";
    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "费用报销单类型明细表", padding = 8)
    private String id;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "reimbursement_item_id", referencedColumnName = "id")
    @Comment("费用报销单")
    private ReimbursementItem reimbursementItem;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "debit_item_id", referencedColumnName = "id")
    @Comment("费用借支单")
    private ExpenseDebitItem expenseDebitItem;


    @Column(name = "written_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("核销金额")
    private double writtenAmount = 0;


    @Column(name = "pend_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    @Comment("待还款金额")
    private double pendAmount = 0.0d;

    @Comment("已还款金额")
    @Column(name = "repayment_amount", nullable = false, columnDefinition = ApesConst.FIELD_MONEY_16_4_0)
    private double repaymentAmount = 0;


    @Transient // 借支金额
    private double expenseAmount = 0.0d;

    public double getExpenseAmount(){
        if (expenseDebitItem==null) return 0.0d;
       return expenseDebitItem.getExpenseAmount();
    }

    @Transient
    private PartyRole borrower;
    public PartyRole getBorrower(){
        if (expenseDebitItem==null) return null;
        return expenseDebitItem.getExpenseDebit().getBorrower();
    }

    @Transient
    private String reason;
    public String getReason(){
        if (expenseDebitItem==null) return null;
        return expenseDebitItem.getExpenseDebit().getReason();
    }

    @Transient
    private String debitId;
    public String getDebitId(){
        if (expenseDebitItem==null) return null;
        return expenseDebitItem.getExpenseDebit().getId();
    }

    @Transient
    private Date documentDate;

    public Date getDocumentDate(){
        if (expenseDebitItem==null) return null;
        return expenseDebitItem.getExpenseDebit().getCreateDate();
    }
}
